package com.leetcode.util;

import com.leetcode.common.ListNode;

public class LinkedListUtil {

    private LinkedListUtil() {

    }

    /**
     * 从 int[] 创建链表
     *
     * @param nums
     * @return
     */
    public static ListNode createList(int[] nums) {
        // 创建虚拟头节点，更容易处理nums为null的情况
        ListNode dummyHead = new ListNode(-1);
        ListNode cur = dummyHead;
        for (int num : nums) {
            cur.next = new ListNode(num);
            cur = cur.next;
        }
        return dummyHead.next;
    }

    /**
     * 控制台打印链表
     *
     * @param head
     */
    public static void printList(ListNode head) {
        while (head != null) {
            System.out.print(head.val + "->");
            head = head.next;
        }
        System.out.print("null");
        // 最后打印一个换行
        System.out.println();
    }

}
